package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.ContractDto;
import com.qf.commerce.common.dto.ContractSelectDto;
import com.qf.commerce.entity.Contract;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.ContractService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
 * @author 358
 * @since JDK 1.8
 * @data 2023/9/15 17:31
 */
@RestController
@RequestMapping("/contract")
public class ContractController {
    @Resource
    private ContractService contractService;

    /**
     * 展示单个合同详情
     *
     * @param contractId contractId
     * @return com.qf.commerce.resp.ResponseResult<com.qf.commerce.entity.Contract>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/show")
    public ResponseResult<Contract> showInfo(Integer contractId) {
        return ResponseResult.success(contractService.selectByPrimaryKey(contractId));
    }

    /**
     * 分页列出所有合同
     *
     * @param page page
     * @param size size
     * @return com.qf.commerce.resp.ResponseResult<com.github.pagehelper.PageInfo < com.qf.commerce.entity.Contract>>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/list")
    public ResponseResult<PageInfo<Contract>> list(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "6") Integer size) {
        try {
            return ResponseResult.success(contractService.list(page, size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 添加合同
     *
     * @param contractDto contractDto
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody ContractDto contractDto) {
        try {
            return ResponseResult.success(contractService.add(contractDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 删除合同
     *
     * @param contractId contractId
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @DeleteMapping("/delete")
    public ResponseResult<Integer> delete(Integer contractId) {
        try {
            return ResponseResult.success(contractService.updateStatus(contractId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 模糊查询
     *
     * @param selectDto selectDto
     * @return com.qf.commerce.resp.ResponseResult<com.github.pagehelper.PageInfo < com.qf.commerce.entity.Contract>>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/select")
    public ResponseResult<PageInfo<Contract>> select(ContractSelectDto selectDto) {
        try {
            return ResponseResult.success(contractService.select(selectDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }
}
